package bb.lanxing.lib.devices.core.scanner;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import bb.lanxing.lib.devices.api.DeviceProvider;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.scanner.DeviceScanner;
import java.lang.ref.WeakReference;
import java.util.UUID;

public class BleSmartDeviceScanner extends BleDeviceScanner {
    private int deviceType;

    @Override
    protected UUID[] getFilterUUIDS() {
        return null;
    }

    public BleSmartDeviceScanner(Context context, DeviceScanner.ScannerCallback<SmartDevice> scannerCallback, DeviceProvider deviceProvider, int i) {
        super(context, new ScannerCallbackWrapper(scannerCallback, deviceProvider));
        this.deviceType = i;
    }

    public BleSmartDeviceScanner(Context context, DeviceScanner.ScannerCallback<SmartDevice> scannerCallback, DeviceProvider deviceProvider, int i, String... strArr) {
        super(context, new ScannerCallbackWrapper(scannerCallback, deviceProvider, strArr));
        this.deviceType = i;
    }

    public BleSmartDeviceScanner(Context context, DeviceScanner.ScannerCallback<SmartDevice> scannerCallback, DeviceProvider deviceProvider) {
        this(context, scannerCallback, deviceProvider, -1);
    }


    private static class ScannerCallbackWrapper implements DeviceScanner.ScannerCallback<ScannedDeviceResult<BluetoothDevice>> {
        private WeakReference<DeviceScanner.ScannerCallback<SmartDevice>> callbackRef;
        private WeakReference<DeviceProvider> deviceProviderRef;
        private String[] nameFiltering;

        public ScannerCallbackWrapper(DeviceScanner.ScannerCallback<SmartDevice> scannerCallback, DeviceProvider deviceProvider) {
            this.nameFiltering = new String[0];
            this.callbackRef = new WeakReference<>(scannerCallback);
            this.deviceProviderRef = new WeakReference<>(deviceProvider);
        }

        public ScannerCallbackWrapper(DeviceScanner.ScannerCallback<SmartDevice> scannerCallback, DeviceProvider deviceProvider, String[] strArr) {
            this.nameFiltering = new String[0];
            this.callbackRef = new WeakReference<>(scannerCallback);
            this.deviceProviderRef = new WeakReference<>(deviceProvider);
            this.nameFiltering = strArr;
        }

        @Override
        public void onStartScan() {
            DeviceScanner.ScannerCallback<SmartDevice> scannerCallback = this.callbackRef.get();
            if (scannerCallback != null) {
                scannerCallback.onStartScan();
            }
        }

        @Override
        public void onStopScan() {
            DeviceScanner.ScannerCallback<SmartDevice> scannerCallback = this.callbackRef.get();
            if (scannerCallback != null) {
                scannerCallback.onStopScan();
            }
        }

        @Override
        public void onDevice(ScannedDeviceResult<BluetoothDevice> scannedDeviceResult) {
            DeviceProvider deviceProvider = this.deviceProviderRef.get();
            DeviceScanner.ScannerCallback<SmartDevice> scannerCallback = this.callbackRef.get();
            if (deviceProvider == null || scannerCallback == null) {
                return;
            }
            BluetoothDevice device = scannedDeviceResult.getDevice();
            String address = device.getAddress();
            @SuppressLint("MissingPermission") String name = device.getName();
            if (!isNameValid(name)) {
                return;
            }
            SmartDevice deviceByAddress = deviceProvider.getDeviceByAddress(address);
            if (deviceByAddress == null) {
                deviceByAddress = deviceProvider.create();
            }
            deviceByAddress.setName(name);
            deviceByAddress.setAddress(address);
            deviceByAddress.setRssi(scannedDeviceResult.getRssi());
            deviceByAddress.setScanRecord(scannedDeviceResult.getScanRecord());
            deviceByAddress.setProtocol(1);
            scannerCallback.onDevice(deviceByAddress);
        }

        private boolean isNameValid(String str) {
            String[] strArr = this.nameFiltering;
            if (strArr.length != 0) {
                for (String str2 : strArr) {
                    if (str.toLowerCase().contains(str2)) {
                        return true;
                    }
                }
                return false;
            }
            return true;
        }
    }
}
